﻿using MIO.Client;
using MIO.Client.Config;
using MIO.Client.Protocal;
using MIOClientTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MIOClientWPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IOClient client;
        int c;
        private readonly object lockObj = new object();
        public MainWindow()
        { 
            InitializeComponent();
            c = 0;
            List<int> ls = new List<int>();
            IOClientConfig conf = new IOClientConfig();
            conf.ServerIp = "192.168.0.116";
            conf.AutoReConnect = true;
            conf.Debug = true;
            conf.MaxReSendCount = 1;
            client = new IOClient(conf);
            client.ConnectSuccess_Event += Client_ConnectSuccess_Event;
            client.Disconnect_Event += Client_Disconnect_Event;
            client.ReconnectSuccess_Event += Client_ReconnectSuccess_Event; 
            client.addEventDelegate("chat", new MIO.Client.Event.EventPacketDelegate<Chat>((client, fromSessionId, n) => {
                Chat c = n;
                inc();
                ReceiveContentRtb.Dispatcher.Invoke(() =>
                {
                    ReceiveContentRtb.AppendText("from :" + fromSessionId + " content:" + c.Content + "\r");
                    //ls.Add(int.Parse(c.Content));
                    ReceiveContentRtb.ScrollToEnd();
                });

                 
                //Console.WriteLine(Thread.CurrentThread.ManagedThreadId+"");
                showCount.Dispatcher.Invoke(()=> {
                    showCount.Text = show()+"";
                });
            }));
        }

        private void inc()
        {
            lock (lockObj)
            {
                c++;
            }
        }

        public int show()
        {
            lock (lockObj)
            {
                return c;
            }
        }

        private void Client_ReconnectSuccess_Event(object sender, EventArgs e)
        {
            string id = "";
            UidTb.Dispatcher.Invoke(()=> {
                id = UidTb.Text;
            });
            ConnectingGrid.Dispatcher.Invoke(() => {
                ConnectingGrid.Visibility = Visibility.Hidden;
            });
            client.sendToServer("join", new JoinRoom()
            {
                ClientType = 1,
                Id = int.Parse(id),
                Name = id,
                RoomId = 1,
                Token = "123456"
            });
        }

        private void Client_Disconnect_Event(object sender, EventArgs e)
        {
            ConnectingGrid.Dispatcher.Invoke(() => {
                ConnectingGrid.Visibility = Visibility.Visible;
            }); 
        }

        private void Client_ConnectSuccess_Event(object sender, EventArgs e)
        {
            string id = "";
            UidTb.Dispatcher.Invoke(() => {
                id = UidTb.Text;
            });
            ConnectingGrid.Dispatcher.Invoke(()=> {
                ConnectingGrid.Visibility = Visibility.Hidden;
            });
            
            client.sendToServer("join", new JoinRoom()
            {
                ClientType = 1,
                Id = int.Parse(id),
                Name = id,
                RoomId = 1,
                Token = "123456"
            });
        }

        private void ConnectionBtn_Click(object sender, RoutedEventArgs e)
        {
            client.connect();
            ConnectingGrid.Dispatcher.Invoke(() => {
                ConnectingGrid.Visibility = Visibility.Visible;
            });
        }

        private void Send_Click(object sender, RoutedEventArgs e)
        {
            string c = InputContentTb.Text;
            OnlineClient oc = OnlineClientCb.SelectedItem as OnlineClient;
            if (oc == null)
            { 
            }
            bool isS= client.sendToClient(oc == null ? "0" : oc.SessionId, "chat", new Chat() { Content=c});
            if (isS)
                MessageBox.Show("send success");
            else
            {
                MessageBox.Show("send fail");
            }
             
        }

        private void GetOnlineClientBtn_Click(object sender, RoutedEventArgs e)
        {
            ResponseGetOnlineClient res = client.sendToServerReceive<ResponseGetOnlineClient>("reqGetOnlineClients", new VoidData(), "respGetOnlineClients");
            if (res != null)
            {
                OnlineClientCb.ItemsSource = res.OnlineClientList;
            }
        }

        private void Send10000_Click(object sender, RoutedEventArgs e)
        {
            string c = InputContentTb.Text;
            OnlineClient oc = OnlineClientCb.SelectedItem as OnlineClient;
            string sessid = "0";
            if (oc == null)
            {
                
            }
            Task.Factory.StartNew(()=> {
                for (int i = 1; i <= 100; i++)
                {
                    bool isS = client.sendToClient(oc==null?"0": oc.SessionId, "chat", new Chat() { Content = i + "" });
                }
            });
             
        }
    }
}
